package com.test.handle;

import com.test.dt0.MyShowResult;
import com.test.dt0.ShowException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 控制器增强   目前用到捕获异常
 * Created by apple on 2017/5/12.
 */
@ControllerAdvice
public class MyHandle {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(value = Exception.class)
    @ResponseBody// 很重要不然没返回了
    public MyShowResult handlerException(Exception e) {
        if (e instanceof ShowException) {
            return new MyShowResult(((ShowException) e).getCode(), e.getMessage());
        }
        logger.error("[系统异常] = {}", e);
        return new MyShowResult(-1, "未知错误");
    }
}
